#!/usr/bin/env python
# -*- coding:utf-8 -*-

# import
import sys
import os
import socket
import re
import fcntl
import struct
import json
import time
import datetime
import threading
import hashlib
import base64
import glob
import urlparse
import urllib
import urllib2

# common
from common.log import log
from common.db.db import Db
from common.const import Const
from common.util import Util

# module
from control.device import Device
from control.stage import Stage
from control.user import User
from control.system import System

defaultencoding = 'utf-8'
if sys.getdefaultencoding() != defaultencoding:
    reload(sys)
    sys.setdefaultencoding(defaultencoding)

vis_device = Device()
vis_stage = Stage()
vis_user = User()
vis_system = System()


class Master:
    '''
    业务逻辑主入口
    '''

    def __init__(self):
        pass

    '''
    以下为设备相关函数
    '''

    def query_devices(self):
        return vis_device.query_devices()

    def query_cameras(self):
        return vis_device.query_cameras()

    def create_device(self, device_name, device_ip, device_type, device_parent):
        return vis_device.create_device(device_name, device_ip, device_type, device_parent)

    def delete_device(self, device_id):
        return vis_device.delete_device(device_id)

    def active_camera(self, device_id):
        return vis_device.active_camera(device_id)

    def inactive_camera(self, device_id):
        return vis_device.inactive_camera(device_id)

    def flv_finish(self, data):
        return vis_device.flv_finish(data)

    def on_connect(self, data):
        return vis_device.on_connect(data)

    def on_close(self, data):
        return vis_device.on_close(data)

    def on_publish(self, data):
        return vis_device.on_publish(data)

    def on_unpublish(self, data):
        return vis_device.on_unpublish(data)

    def on_play(self, data):
        return vis_device.on_play(data)

    def on_stop(self, data):
        return vis_device.on_stop(data)

    def on_hls_notify(self, stream, ts_name):
        return vis_device.on_hls_notify(stream, ts_name)

    '''
    以下为舞台相关函数
    '''

    def query_stage(self):
        return vis_stage.query_stage()

    def set_stage_split(self, split):
        vis_device.clear_all_pull_rtsp()
        return vis_stage.set_stage_split(split)

    def set_stage_subscreen(self, pos, device_id):
        old_device_id = vis_stage.get_device_id_by_pos(pos)
        if old_device_id != -1:
            vis_device.stop_pull_rtsp(old_device_id)
        if device_id != -1:
            vis_device.start_pull_rtsp(device_id)
        return vis_stage.set_stage_subscreen(pos, device_id)

    '''
    以下为用户相关函数
    '''

    def query_users(self):
        return vis_user.query_users()

    def add_user(self, username, nickname, passwd, phone, email):
        return vis_user.add_user(username, nickname, passwd, phone, email)

    def delete_user(self, username):
        return vis_user.delete_user(username)

    def login(self, username, passwd):
        return vis_user.login(username, passwd)

    def modify_password(self, username, old_passwd, new_passwd):
        return vis_user.modify_password(username, old_passwd, new_passwd)

    '''
    以下为配置相关函数
    '''

    def get_configs(self):
        ret_code = 1000
        ret_data = None

        ret_data = Util.get_configs()

        return ret_code, ret_data

    def get_config(self, config_key):
        ret_code = 1000
        ret_data = None

        ret_data = Util.get_config(config_key)

        return ret_code, ret_data

    def set_config(self, config_key, config_value):
        ret_code = 1000
        ret_data = None

        Util.set_config(config_key, config_value)

        return ret_code, ret_data

    '''
    以下为系统相关函数
    '''

    def query_system(self):
        return vis_system.query_system()
